package com.ctg.itrdc.imageconsole.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.dto.SystemUserDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoginFilter  implements Filter {

	private Logger LOGGER = LoggerFactory.getLogger(this.getClass());

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		/*// 获得在下面代码中要用的request,response,session对象
	    HttpServletRequest servletRequest = (HttpServletRequest) request;
	    HttpServletResponse servletResponse = (HttpServletResponse) response;
	    HttpSession session = servletRequest.getSession();
	    // 获得用户请求的URI
	    String path = servletRequest.getRequestURI();

	    //跳过验证的URL
	    if(path.indexOf("/aePortal/dist") > -1 || path.indexOf("/sys/sysuser/login") > -1 ) {
	    	chain.doFilter(servletRequest, servletResponse);
	    	return;
		}
	     
		//判断用户是否已经登录
	    if((SystemUserDTO)servletRequest.getSession().getAttribute("sysUser") == null){  //未登录跳到登录页面
			if(path.indexOf(".html") > -1 || path.indexOf(".jsp") > -1){// 如果是前端网页访问，则跳转至首页
				LOGGER.info("页面请求,会话超时或不存在,返回首页!");
				servletResponse.sendRedirect("/aePortal/dist/#/");
				return;
			}

			 // 如果是ajax请求，则返回json数据
			LOGGER.info("ajax请求,会话超时或不存在,返回首页!");
			CommonResult commonResult = new CommonResult(ReturnCodeDefine.Code.C1201, ReturnCodeDefine.Msg.M1201);
			servletResponse.sendRedirect("/aePortal/dist/#/");
	//		response.getWriter().append(JSONUtils.ToJsonString(commonResult)).close();
			return;
	    }

		chain.doFilter(request, response);*/
	}
	

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}